<template>
	<view class="container">
		<navbar title="商城分类" color="#333333">
			<view class="left-icon" slot="leftIcon" style="position: absolute;">
				<view class="logo">
					<image
						:src="logo"
						mode="aspectFit"
					/>
				</view>
			</view>
		</navbar>
		<view class="search">
			<headerSearch placeholder="输入关键词进行搜索" bgColor="#F5F5F5" @searchClick="searchClick"></headerSearch>
		</view>
		<view class="content">
			<view class="list_left">
				<view class="item one-row" :class="class_id==item.id ? 'select':''" v-for="(item,i) in classifyList" :key="item.id" @click="classifyChange(item)">
					{{item.name}}
				</view>
			</view>
			<view class="list_right">
				<view class="screen">
					<view class="tab">
						<view class="tab-item" :class="tab==1 ? 'select' : ''" @click="tabClick(1)">
							<text>综合</text>
						</view>
						<view class="tab-item" :class="tab==2 ? 'select' : ''" @click="tabClick(2,sort)">
							<text>价格</text>
							<image v-if="sort_price==1" :src="$img_path('/course/sort_1.png')" mode="widthFix"></image>
							<image v-if="sort_price==2" :src="$img_path('/course/sort_2.png')" mode="widthFix"></image>
						</view>
						<view class="tab-item" :class="tab==3 ? 'select' : ''" @click="tabClick(3,sort)">
							<text>销量</text>
							<image v-if="sort_num==1" :src="$img_path('/course/sort_1.png')" mode="widthFix"></image>
							<image v-if="sort_num==2" :src="$img_path('/course/sort_2.png')" mode="widthFix"></image>
						</view>
						<view class="tab-item box-style" @click="tabClick(4,box_style)">
							<image v-if="box_style==1" :src="$img_path('/course/s_i_1.png')" mode="widthFix"></image>
							<image v-if="box_style==2" :src="$img_path('/course/s_i_2.png')" mode="widthFix"></image>
						</view>
					</view>
				</view>
					<scroll-view scroll-y="true" :style="{'--m-h':list_m_h+'px'}" >
				<view class="goodsList">
						<view class="item-box" :class="box_style==2 ? 'column-class' : 'row-class' " v-for="(item,i) in goodsList" :key="item.id" :style="{width: box_style==2 ? '100%' : '258rpx'}">
							<shopGoods :styleType="box_style" 
							:goodsDetail="item" 
							 :pageUrl="'/pagesMall/goods/detail?id='+item.id"
							:imageStyle="{width:box_style==2 ? '232rpx' : '258rpx',height:box_style==2 ? '232rpx' : '232rpx'}">
								<text slot="center" style="margin-top: 16rpx;display: block;">销量：{{item.sales}}</text>
								<view slot="tagList" class="tagList" v-if="item.labels">
									<view class="tag-item" v-for="(t_i,idx) in item.labels.split(',')" :key="idx">
										{{t_i}}
									</view>
								</view>
								<view slot="tagList" class="tagList" v-else>
									<view style="background:transparent;border:none" class="tag-item" v-for="(t_i,idx) in item.labels.split(',')" :key="idx">
										{{t_i}}
									</view>
								</view>
							</shopGoods>
						</view>
				</view>
					</scroll-view>
			</view>
		</view>
		<tarbar index="2" page="3"></tarbar>
	</view>
</template>

<script>
	import {categoryNew, listsNew} from '@/utils/api/mallApi.js'
	import headerSearch from '../../components/header-search/header-search.vue'
	import shopGoods from '@/components/service-box/mall-item-box.vue'
	export default {
		components: {
			headerSearch,
			shopGoods
		},
		data() {
			return {
				list_m_h:this.statusBarHeight+this.bottomHeight+this.navBarHeight,
				sort_price: 1,
				sort_num: 1,
				classifyList:[],
				class_id:null,
				
				tab:1,
				box_style:1,
				keywords:'',
				goodsList:[],
				logo: uni.getStorageSync("logo"),
			}
		},
		onLoad() {
			this.getClassify()
		},
		methods:{
			searchBlur(val){
				this.keywords = val.detail.value
				this.getGoodsList()
			},
			
			searchClick(){
				uni.navigateTo({
					url:'/pagesMall/search/search'
				})
			},
			
			async getClassify(){
				let data = await categoryNew()
				this.classifyList = data.data
				this.class_id = data.data[0].id
				this.getGoodsList()
			},
			classifyChange(data){
				this.class_id = data.id
				this.getGoodsList()
			},
			
			getGoodsList(){
				let obj = {
					category_id:this.class_id,
					keywords:this.keywords,
					sort:this.tab==2?'price':this.tab==3?'sales':'',
					order: this.tab==2 ? (this.sort_price==1 ? 'desc' : 'ace') : this.tab==3 ? (this.sort_num==1 ? 'desc' : 'ace') : '',
					type:'goods'
				}
				listsNew(obj).then(res=>{
					// console.log(res,'-')
					this.goodsList = res.data.data
				})
			},
			
			tabClick(type,val){
				switch (type){
					case 1:
						break;
					case 2:
						if(this.tab==type){
							if(this.sort_price == 1){
								this.sort_price = 2
							}else{
								this.sort_price = 1
							}
						}
						break;
					case 3:
						if(this.tab==type){
							if(this.sort_num == 1){
								this.sort_num = 2
							}else{
								this.sort_num = 1
							}
						}
						break;
					default:
						if(this.box_style==1){
							this.box_style = 2
						}else{
							this.box_style = 1
						}
						break;
				}
				if(type!=4){
					this.tab = type
				}
				
				this.getGoodsList()
			},
		}
	}
</script>

<style lang="scss" scoped>
.container{
	
		.left-icon{
			left: 22rpx;
			width: 142rpx;
			height: 68rpx;
			background: transparent;
			box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(0,0,0,0.04);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	
	// padding: 20rpx 30rpx 0;
	box-sizing: border-box;
	.search{
		padding: 20rpx 22rpx 34rpx;
		box-sizing: border-box;
		background-color: #FFFFFF;
	}
	.content{
		width: 100%;
		// margin-top: 32rpx;
		display: flex;
		.list_left{
			width: 174rpx;
			height: calc(100vh - 190rpx - var(--m-h));
			overflow-y: auto;
			margin-top: -28rpx;
			z-index: 99;
			.item{
				width: 100%;
				height: 130rpx;
				font-size: 32rpx;
				color: #333;
				display: flex;
				align-items: center;
				justify-content: center;
				&.select{
					background-color: #FFFFFF;
					color: #E72A2A;
				}
			}
		}
		.list_right{
			width: calc(100% - 174rpx);
			padding: 0 22rpx;
			box-sizing: border-box;
			background-color: #FFFFFF;
			.screen {
				// padding: 30rpx 0rpx 0;
				.tab {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 16rpx 24rpx 14rpx;
					// background-color: #FFFFFF;
					border-radius: 10rpx;
					// margin-bottom: 32rpx;
					.tab-item {
						display: flex;
						justify-content: center;
						align-items: center;
						text {
							font-weight: bold;
							font-size: 32rpx;
							color: #999999;
							line-height: 44rpx;
						}
						&.select {
							text {
								color: #E72A2A;
							}
						}
			
						image {
							width: 30rpx;
							height: 30rpx;
						}
					}
				}
			}
			
				scroll-view{
					width: 100%;
					height: calc(100vh - 296rpx - var(--m-h));
				}
			.goodsList{
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				.item-box{
					box-sizing: border-box;
					margin-bottom: 14rpx;
					display: flex;
					flex-direction: column;
					&.column-class{
						padding: 20rpx 0;
						border-bottom: 2rpx solid #F8F8F8;
						&:first-child{
							padding-top: 0;
						}
					}
					&.row-class{
						border: 2rpx solid #F3F3F3;
						border-radius: 20rpx;
					}
					.tagList{
						display: flex;
						align-items: center;
						flex-wrap: wrap;
						.tag-item{
							// width: 118rpx;
							height: 34rpx;
							border-radius: 6rpx 6rpx 6rpx 6rpx;
							border: 2rpx solid #0291FF;
							font-weight: 400;
							font-size: 20rpx;
							margin-top: 16rpx;
							color: #0291FF;
							// line-height: 34rpx;
							text-align: center;
							padding: 3rpx 12rpx;
							box-sizing: border-box;
							margin-left: 12rpx;
							&:first-child{
								background: #FF6602;
								border-color: #FF6602;
								color: #FFFFFF;
								margin-left: 0;
							}
						}
					}
				}
			}
		}
	}
}

.logo{
	
  image{
    width: 54rpx;
    // height: 44rpx;
  }
}
/deep/.left-slot{
	padding-left: 0!important;
}
</style>
